package com.base.exp

import android.content.res.Resources
import android.view.View
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.RecyclerView

fun View.delayClick(duration: Long = 1000L) {
    isClickable = false
    postDelayed({ isClickable = true }, duration)
}

fun Float.dp2Px(): Int {
    val density = Resources.getSystem().displayMetrics.density
    return (this * density + 0.5f * if (this >= 0) 1 else -1).toInt()
}

fun RecyclerView.scroll2Last() {
    val adapter = adapter ?: return
    //smoothScrollToPosition(adapter.itemCount - 1)
    scrollToPosition(adapter.itemCount - 1)
}

fun View.applyWindowInsets() {
    ViewCompat.setOnApplyWindowInsetsListener(this) { v, insets ->
        val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
        v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
        insets
    }
}

